package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @RequestMapping("/list")
    //@RequiresPermissions(value ={"department:list,department:view"},logical = Logical.AND)
//    @RequiresRoles("devGr")
    @RequiresPermissions("department:list")
    public String list(Model model,QueryObject qo) {
        PageInfo<Department> pageInfo = departmentService.query(qo);
        System.out.println(pageInfo.toString());
        model.addAttribute("pageInfo", pageInfo);
        return "department/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions("department:delete")
    public String delete(Long id) {
        if (id != null && !id.equals("")) {
            departmentService.delete(id);
        }
        return "redirect:/department/list";  //返回的是上面查询所偶有所的方法
    }

//    @RequestMapping("/input")
//    public String input(Long id, Model model) {
//        if (id != null && !id.equals("")) {
//            Department department = departmentService.get(id);
//            model.addAttribute("department", department);
//        }
//        return "department/input";
//
//    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions("department:saveOrUpdate")
    public String saveOrUpdate(Department department) {
        Long id = department.getId();
        if (id != null && !id.equals("")) {
            departmentService.update(department);
        }else {
            departmentService.save(department);
        }
        return "redirect:/department/list";
    }

}
